const path = require('path')
const merge = require('webpack-merge')

function resolve(dir) {
  console.log(__dirname)
  return path.join(__dirname, dir)
}

const params = {
  publicPath: '/', // 资源加载路径目前是绝对路径 ./则是相对路径
  outputDir: 'dist', // 打包输出地址 默认dist
  assetsDir: 'assets',
  lintOnSave: true,
  pages: {
    login: {          
      entry: 'src/login.js',          
      template: 'public/login.html', 
      filename: "login.html",       
    },        
    index: {          
      entry: 'src/main.js',          
      template: 'public/index.html',
      filename: "index.html",    
    }, 
  },
  transpileDependencies: [],
  integrity: false,
  filenameHashing: true,
  runtimeCompiler: false,
  productionSourceMap: false,
  configureWebpack: config => {
    config.resolve.alias = merge(config.resolve.alias, {
      '@': resolve('src'),
      src: path.resolve(__dirname, '.', 'src'),
      assets: path.resolve(__dirname, '.', 'src/assets'),
      components: path.resolve(__dirname, '.', 'src/components')
    })
  },
  css: {
    loaderOptions: {
      scss: {
        prependData: `@import "~assets/scss/variables.scss";`
      }
    }
  },
  chainWebpack(config) {
    config.plugins.delete("preload"); // TODO: need test
    config.plugins.delete("prefetch"); // TODO: need test

    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons/svg'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })

    // set preserveWhitespace
    config.module
      .rule("vue")
      .use("vue-loader")
      .loader("vue-loader")
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true;
        return options;
      })
      .end();

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              maxInitialRequests: 4,
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial'
                },
                elementUI: {
                  name: 'chunk-elementUI',
                  priority: 20,
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'),
                  minChunks: 3,
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}

if (process.env.NODE_ENV !== 'production') {
  params['devServer'] = {
    port: 8880,
    proxy: {
      '/': {
        // 设置请求的地址 用于反向代理请求 
        target: 'http://localhost:8080/'
        //
      }
    }
  }
}
module.exports = params
console.log('-----------------')
console.log(process.env.NODE_ENV)
console.log('-----------------')
